Public Class adduser
    Inherits System.Web.UI.Page
    'Protected WithEvents TheHeader1 As adm_header
    Public Msg, target, sortby, sPin As String
    Public aUserSession(13) As String
    'Public oUtils As utils
    Protected WithEvents UID As System.Web.UI.WebControls.TextBox
    Protected WithEvents Password As System.Web.UI.WebControls.TextBox
    Protected WithEvents PasswordConfirm As System.Web.UI.WebControls.TextBox
    Protected WithEvents firstname As System.Web.UI.WebControls.TextBox
    Protected WithEvents lastname As System.Web.UI.WebControls.TextBox
    Protected WithEvents location As System.Web.UI.WebControls.TextBox
    Protected WithEvents Email As System.Web.UI.WebControls.TextBox
    Protected WithEvents Phone As System.Web.UI.WebControls.TextBox
    Protected WithEvents btnCreate As System.Web.UI.WebControls.Button
    Protected WithEvents btnCancel As System.Web.UI.WebControls.Button
    Protected WithEvents drpSuperv As System.Web.UI.WebControls.DropDownList
    Public aUsersSorted As Array
    Public objValid As New gaValidation
    Public Const cSESSION_ID = 0
    Public Const cCOMPANY_ID = 1
    Public Const cGAPORTAL_ID = 2
    Public Const cUSER_ID = 3
    Public Const cINCIDCONTEXT = 4
    Public Const cINGIDCONTEXT = 5
    Public Const cLOGINDATETIME = 6
    Public Const cLASTACTIVITY = 7
    Public Const cUSERPERMISSIONSSTRING = 8
    Public Const cROLE_ID = 9
    Public Const cMEMBER_TYPE = 10
    Public Const cTERMINAL_NUMBER = 11
    Public Const cTERMINAL_MARKET = 12
    Public LevelAdmin, AdminGroupSuper, sLevelMarker As String
    Public sHTML As String = ""
    Public supervisor As String
    Public sVID, sUID As String
    'Dim objValid As gaValidation
    Public imgpath, currtURL As String
    Protected WithEvents drpCountry As System.Web.UI.WebControls.DropDownList
    Public oSupervisors As UserManagement.UserManagement
#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here
        objValid = New gaValidation
        objValid.getURL(Request.Url.ToString, imgpath, currtURL)

        'GetSuppervisorList()
    End Sub
    Public Function GetSuppervisorList() As String
        oSupervisors = New UserManagement.UserManagement
        oSupervisors.DBConnectstring = Application("DSN")
        Dim supervisor As String = ""
        'Dim objValid As gaValidation
        Dim LevelAdmin As String = ""
        Dim AdminGroupSuper As String = ""
        Dim sLevelMarker As String = ""
        Dim sHTML As String = ""
        Dim i As Integer
        aUsersSorted = oSupervisors.GetMerchantUsers(aUserSession(cINGIDCONTEXT), supervisor)
        'oSupervisors = Nothing

        '       '--- CREATE DROP DOWN LIST
        If Not IsNothing(aUsersSorted) Then
            sHTML = ""
            sHTML = sHTML & "<select name=""Supervisor"">"

            '--- LOOPS THROUGH ARRAY FOR EACH ELEMENT
            '--- aUsersSorted(i,0) : CONTAINS THE UID OF EACH POTENTIAL SUPERVISOR
            '--- aUsersSorted(i,1) : CONTAINS UID OF SUPERVISOR OF PERSON SHOWN IN (i,0)
            '--- aUsersSorted(i,2) : CONTAINS AN 'ACCESS' FLAG -- NOT USED IN THIS CONTEXT
            '--- aUsersSorted(i,3) : CONTAINS A USER LEVEL FLAG
            '--------------------------------------------------------

            For i = 0 To UBound(aUsersSorted)
                If aUsersSorted(i, 0) = aUserSession(cUSER_ID) Then
                    LevelAdmin = Trim(aUsersSorted(i, 1))
                    Exit For
                End If
            Next

            If Len(LevelAdmin) = 0 Then
                LevelAdmin = aUserSession(cUSER_ID)
            End If

            For i = 0 To UBound(aUsersSorted)
                If aUsersSorted(i, 0) = LevelAdmin Then
                    AdminGroupSuper = Trim(aUsersSorted(i, 1))
                    sLevelMarker = New String(aUsersSorted(i, 3) * 2, "^")
                    Exit For
                End If
            Next

            'response.write ("<p><b>Management Hierarchy:</b><br>")

            sHTML = sHTML & vbTab & vbTab & vbTab & vbTab & vbTab & vbTab & "<option value=""" & LevelAdmin & """>" & sLevelMarker & LevelAdmin & "</option>" & vbCrLf
            ''drpSuperv.Items.Clear()
            FindSubordinates(LevelAdmin)

            'response.write ("<p>&nbsp;")

            sHTML = sHTML & vbTab & vbTab & vbTab & vbTab & vbTab & "</select>"
            sHTML = Replace(sHTML, "^", "&nbsp;")
            '--- END DROP DOWN LIST

            '--- IF ANYTHING WAS ADDED TO THE DROP DOWN LIST THEN DISPLAY THE LIST
            '--- OTHERWISE DISPLAY NOTHING
            'Response.Write(sHTML)

        Else
            ' --- aUsersSorted IS NOT AN ARRAY %>
            'Could not retrieve user list.
            '<input type="hidden" name="supervisor" value="<%=Supervisor%>">
        End If
        Return sHTML

    End Function
    Private Sub FindSubordinates(ByVal Manager As String)
        Dim indx1 As Integer
        For indx1 = 0 To UBound(aUsersSorted)
            If aUsersSorted(indx1, 1) = Manager Then
                sLevelMarker = New String(aUsersSorted(indx1, 3) * 2, "^")
                'response.write (Manager & " manages " & aUsersSorted(indx1, 0) & "<br>")
                'if (aUsersSorted(indx1,0) <> sUID and safestring(aUsersSorted(indx1,1)) <> sUID) then
                If aUsersSorted(indx1, 0) = supervisor Then
                    sHTML = sHTML & vbTab & vbTab & vbTab & vbTab & vbTab & vbTab & "<option value=""" & aUsersSorted(indx1, 0) & """ selected>" & sLevelMarker & aUsersSorted(indx1, 0) & "</option>" & vbCrLf
                Else
                    sHTML = sHTML & vbTab & vbTab & vbTab & vbTab & vbTab & vbTab & "<option value=""" & aUsersSorted(indx1, 0) & """>" & sLevelMarker & aUsersSorted(indx1, 0) & "</option>" & vbCrLf
                End If

                FindSubordinates(aUsersSorted(indx1, 0))
                'end if
                sLevelMarker = ""
            End If
        Next
    End Sub

    Private Sub btnCreate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreate.Click
        Dim validentry As Boolean = "false"
        Dim sUID, sVID, Email As String
        Dim country, phone_nbr, active As String
        Dim nCID As Integer
        Dim validpwd, Addok As Boolean
        Dim fname, lname As String
        Dim pwd1, pwd2 As String
        Dim oAdduser As UserManagement.UserManagement
        objValid = New gaValidation
        sUID = Trim(UID.Text)
        sVID = aUserSession(cINGIDCONTEXT)
        nCID = CInt(aUserSession(cINCIDCONTEXT))
        Email = Trim(Request("Email"))
        'oValidator = nServer.CreateObject("viaFormValidator.General")

        pwd1 = Trim(Password.Text)
        pwd2 = Trim(PasswordConfirm.Text)
        If Len(pwd1) = 0 Then
            Msg = "Password is required."
        ElseIf Len(pwd1) < 7 Or Len(pwd2) < 7 Then

            Msg = "Please enter a minimum of seven(7) characters. Your password must contain at least one letter and one number."
        ElseIf pwd1 <> pwd2 Then
            Msg = "Password is required."
        Else
            validpwd = objValid.checkPwd(pwd1)
        End If

        If sUID = vbNullString Or Len(sUID) > 15 Then
            Msg = "Enter a User ID that contains less than 16 characters."
        End If

        If Msg = "" And validpwd = True Then

            oAdduser = New UserManagement.UserManagement
            oAdduser.DBConnectString = Application("DSN")
            oAdduser.SESSION_VIAKLIX_ID = aUserSession(cGAPORTAL_ID)
            oAdduser.SESSION_USER_ID = aUserSession(cUSER_ID)
            oAdduser.SESSION_COMPANY_ID = aUserSession(cCOMPANY_ID)
            '-----	BW 1/16/02 server ip, server time, and source ip auditing
            oAdduser.ServerIP = Trim(Request.ServerVariables("LOCAL_ADDR"))
            oAdduser.ServerTime = Trim(Now())
            oAdduser.SourceIP = Trim(Request.ServerVariables("REMOTE_ADDR"))
            '-----	end server ip, server time, and source ip auditing
            supervisor = Trim(Request("Supervisor"))
            fname = Trim(firstname.Text)
            lname = Trim(lastname.Text)
            country = Trim(drpCountry.SelectedItem.Text)
            Phone_nbr = Trim(phone.text)
            ''Ext = safestring(Request("Ext"))
            'Active=BoolToYN(request("Active"))
            Active = "Y"
            Addok = oAdduser.AddUser(sUID, sVID, nCID, pwd1, fname, lname, country, phone_nbr, Email, supervisor, active)
            If Addok <> "OK" Then
                Msg = Addok
            End If
        End If


    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Response.Redirect("user_default.aspx")
    End Sub

    Private Sub drpSuperv_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles drpSuperv.SelectedIndexChanged

    End Sub
End Class
